using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using SellAvi.Classes;

namespace SellAvi.Model
{
    [Table("AvitoProducts")]
    public partial class AvitoProduct : AbstractModel
    {
        [SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public AvitoProduct()
        {
            AvitoImages = new HashSet<AvitoImage>();
        }

        public int Id { get; set; }

        [StringLength(4000)]
        [ExportAllowed(true)]
        public string Title { get; set; }

        [StringLength(4000)]
        [ExportAllowed(true)]
        public string Description { get; set; }

        [StringLength(4000)]
        [ExportAllowed(true)]
        public string Price { get; set; }

        [StringLength(4000)]
        [ExportAllowed(true)]
        public string ParsedFromUrl { get; set; }

        [StringLength(4000)]
        [ExportAllowed(true)]
        public string ParsedPhone { get; set; }

        [NotMapped]
        public string BreadCrumbsCategory { get; set; }

        public DateTime? DateInsert { get; set; }

        [StringLength(4000)]
        public string PostedOnUrl { get; set; }

        public DateTime? DatePosted { get; set; }
        public bool IsProfileProduct { get; set; }
        [StringLength(4000)]
        public string YouTubeUrl { get; set; }

        public int TCategory_Id { get; set; }

        public int? TParam_Id { get; set; }

        public int TRegion_Id { get; set; }

        public int AvitoUser_Id { get; set; }

        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<AvitoImage> AvitoImages { get; set; }
        [NotMapped]
        [ExportAllowed(true)]
        public string MainProductImage
        {
            get
            {
                var mainImage = AvitoImages.FirstOrDefault(i => i.IsMainImage == true) ?? AvitoImages.LastOrDefault();
                if (mainImage != null && mainImage.LocalFileName != null && File.Exists("images/" + mainImage.LocalFileName))
                    return Path.GetFullPath("images/" + mainImage.LocalFileName);
                if (mainImage != null && mainImage.RemoteFileUrl != null)
                {
                    return mainImage.RemoteFileUrl;
                }
                //BUG: System.Windows.Data Error: 23 : Cannot convert '<null>' from type '<null>' to type 'System.Windows.Media.ImageSource' for 'en-US'
                return null;
            }
        }

        [NotMapped]
        [ExportAllowed(true)]
        public string MainProductImageUrl
        {
            get
            {
                var firstImage = AvitoImages.LastOrDefault();
                if (firstImage != null)
                    return firstImage.RemoteFileUrl;
                return null;
            }
        }

        public virtual AvitoUser AvitoUser { get; set; }

        public virtual TCategory Category { get; set; }

        public virtual TParam Param { get; set; }

        public virtual TRegion Region { get; set; }
    }
}
